<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 反向代理深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: attr(data-language);
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            font-size: 0.75rem;
            color: #858585;
            text-transform: uppercase;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.25rem;
            font-weight: 700;
            color: #667eea;
        }
        
        .section-divider {
            width: 60px;
            height: 4px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            margin: 2rem auto;
            border-radius: 2px;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fade-in">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-server mr-4"></i>Nginx 反向代理
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解正向代理与反向代理的本质区别</p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-shield-alt mr-2"></i>安全防护
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-balance-scale mr-2"></i>负载均衡
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-tachometer-alt mr-2"></i>性能优化
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">正</span>向代理和反向代理有不同的用途和配置方式。Nginx 主要用于配置反向代理，通过设置 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">proxy_pass</code> 指令以及其他代理相关指令，可以实现多种高级功能如负载均衡、缓存和 SSL 终端等。了解如何配置这些功能可以帮助提升服务器的性能和安全性。
            </p>
        </section>

        <!-- Proxy Comparison -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">
                <span class="text-gradient">代理模式对比</span>
            </h2>
            <div class="section-divider"></div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Forward Proxy -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-user-shield text-2xl text-blue-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold">正向代理</h3>
                    </div>
                    
                    <div class="space-y-4">
                        <div class="border-l-4 border-blue-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-info-circle mr-2 text-blue-500"></i>定义
                            </h4>
                            <p class="text-gray-600">正向代理是客户端使用代理服务器访问互联网的一种方式。客户端将请求发送到代理服务器，然后由代理服务器转发到目标服务器，并将目标服务器的响应返回给客户端。</p>
                        </div>
                        
                        <div class="border-l-4 border-green-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-cogs mr-2 text-green-500"></i>作用
                            </h4>
                            <p class="text-gray-600">隐藏客户端的真实 IP 地址、绕过地理限制、缓存请求、进行访问控制等。</p>
                        </div>
                        
                        <div class="border-l-4 border-purple-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-globe mr-2 text-purple-500"></i>使用场景
                            </h4>
                            <p class="text-gray-600">常用于内部网络环境中，访问外部互联网资源。</p>
                        </div>
                    </div>
                </div>

                <!-- Reverse Proxy -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-6">
                        <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-server text-2xl text-purple-600"></i>
                        </div>
                        <h3 class="text-2xl font-bold">反向代理</h3>
                    </div>
                    
                    <div class="space-y-4">
                        <div class="border-l-4 border-blue-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-info-circle mr-2 text-blue-500"></i>定义
                            </h4>
                            <p class="text-gray-600">反向代理是服务器端使用代理服务器处理客户端请求的一种方式。客户端的请求首先到达代理服务器，由代理服务器将请求转发到实际的后台服务器，并将后台服务器的响应返回给客户端。</p>
                        </div>
                        
                        <div class="border-l-4 border-green-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-cogs mr-2 text-green-500"></i>作用
                            </h4>
                            <p class="text-gray-600">隐藏真实服务器的 IP 地址、负载均衡、SSL 终端、缓存静态内容、压缩传输等。</p>
                        </div>
                        
                        <div class="border-l-4 border-purple-500 pl-4">
                            <h4 class="font-semibold text-gray-800 mb-2">
                                <i class="fas fa-globe mr-2 text-purple-500"></i>使用场景
                            </h4>
                            <p class="text-gray-600">常用于互联网环境中，提供对内部服务器的统一访问入口。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visual Diagram -->
        <section class="bg-gray-50 rounded-2xl p-8 mb-16">
            <h3 class="text-2xl font-bold mb-6 text-center">代理模式架构图</h3>
            <div class="mermaid">
                graph LR
                    subgraph "正向代理"
                        C1[客户端1] --> FP[正向代理服务器]
                        C2[客户端2] --> FP
                        C3[客户端3] --> FP
                        FP --> I1[互联网]
                        I1 --> S1[目标服务器]
                    end
                    
                    subgraph "反向代理"
                        U1[用户1] --> I2[互联网]
                        U2[用户2] --> I2
                        U3[用户3] --> I2
                        I2 --> RP[反向代理服务器]
                        RP --> B1[后端服务器1]
                        RP --> B2[后端服务器2]
                        RP --> B3[后端服务器3]
                    end
                    
                    style FP fill:#667eea,stroke:#fff,color:#fff
                    style RP fill:#764ba2,stroke:#fff,color:#fff
            </div>
        </section>

        <!-- Configuration Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">
                <span class="text-gradient">反向代理配置指南</span>
            </h2>
            <div class="section-divider"></div>

            <!-- Basic Configuration -->
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <h3 class="text-2xl font-bold mb-6">
                    <i class="fas fa-wrench mr-3 text-purple-600"></i>基本反向代理配置
                </h3>
                
                <p class="text-gray-700 mb-6">在 Nginx 中配置反向代理相对简单，主要通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">proxy_pass</code> 指令来实现。以下是配置步骤：</p>

                <!-- Steps -->
                <div class="space-y-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">1</div>
                        <div class="flex-grow">
                            <h4 class="font-semibold text-lg mb-2">安装和启动 Nginx</h4>
                            <p class="text-gray-600">确保 Nginx 已安装并正在运行。</p>
                        </div>
                    </div>

                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-600 text-white rounded-full flex items-center justify-center font-bold mr-4">2</div>
                        <div class="flex-grow">
                            <h4 class="font-semibold text-lg mb-2">编辑 Nginx 配置文件</h4>
                            <p class="text-gray-600 mb-4">配置文件通常位于 <code class="bg-gray-100 px-2 py-1 rounded">/etc/nginx/nginx.conf</code> 或 <code class="bg-gray-100 px-2 py-1 rounded">/etc/nginx/sites-available/default</code>。</p>
                            
                            <div class="code-block" data-language="nginx">
                                <pre><code>server {
    listen 80;
    server_name toString.com;

    location / {
        # 反向代理的目标服务器地址
        proxy_pass http://backend_server;
        
        # 设定请求头部
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Optional: 设置代理超时
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_rea